//
//  SDServerActionView.m
//  SBooks
//
//  Created by shansander on 2016/11/26.
//  Copyright © 2016年 shansander. All rights reserved.
//

#import "SDServerActionView.h"
#import "AppMacro.h"
#import "SDHttpServerManager.h"
#import "HTTPServer.h"

CGFloat const height_serveraction = 250.f;

@implementation SDServerActionView
{
    UILabel * tempLabel;
    UILabel * theInfoLabel;
}


- (instancetype)init
{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, screenHeight, screenWidth, height_serveraction);
        [self configView];
    }
    return self;
}

- (void)configView
{
    
    self.layer.masksToBounds = true;
    self.layer.cornerRadius = 5.0f;
    
    [self setBackgroundColor:[UIColor whiteColor]];
    closeButton = [[UIButton alloc] initWithFrame:CGRectMake(10, height_serveraction - 50, screenWidth - 20, 40)];
    
    [closeButton addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
    NSString * close_string = [InternationalControl localizedStringForKey:@"KeyClose"];
    [closeButton setTitle:close_string forState:UIControlStateNormal];
    [closeButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [closeButton setBackgroundColor:[AppTheme getBackThemeColor]];
    [self addSubview:closeButton];
    
    closeButton.layer.masksToBounds = true;
    closeButton.layer.cornerRadius = 5.0f;
    
    
    
    UIImage * wifi_image = [UIImage imageNamed:@"wificonnect"];
    
    UIImageView * rootImageView = [[UIImageView alloc] initWithImage:wifi_image];
    
    rootImageView.frame = CGRectMake(0, 0, 81, 64);
    rootImageView.center = CGPointMake(self.bounds.size.width/2.f, 45);
    [self addSubview:rootImageView];
    
    
    tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(rootImageView.frame) + 10, self.bounds.size.width, 40)];
    tempLabel.text = [InternationalControl localizedStringForKey:@"Keysafarilocal"];
    tempLabel.textAlignment = NSTextAlignmentCenter;
    tempLabel.font = [UIFont systemFontOfSize:15];
    tempLabel.numberOfLines = 0;
    [self addSubview:tempLabel];
    
    ip_adress_label = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(tempLabel.frame) + 10, self.bounds.size.width, 30)];
    ip_adress_label.text = @"http:0.0.0.0:40000";
    ip_adress_label.font = [UIFont systemFontOfSize:22];
    ip_adress_label.textAlignment = NSTextAlignmentCenter;
    [ip_adress_label setTextColor:[UIColor greenColor]];
    [self addSubview:ip_adress_label];
    
    theInfoLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMinY(closeButton.frame) - 30, self.bounds.size.width, 30)];
    theInfoLabel.text = [InternationalControl localizedStringForKey:@"Keyinfostring"];
    theInfoLabel.textColor = [UIColor grayColor];
    theInfoLabel.font = [UIFont systemFontOfSize:12];
    theInfoLabel.textAlignment = NSTextAlignmentCenter;
    theInfoLabel.numberOfLines = 0;
    [self addSubview:theInfoLabel];
    
    
    
}

- (void)startAction
{
    [UIView animateWithDuration:0.3 animations:^{
        self.frame = CGRectMake(0, screenHeight - height_serveraction, screenWidth, height_serveraction);
    }];
    
    [self startServer];
}

- (void)startServer
{
    
    if ([[SDHttpServerManager getHttpServer] GetCurntNet]) {
        [[SDHttpServerManager getHttpServer] startServerFinish:^(HTTPServer *hs) {
            ip_adress_label.text = [NSString stringWithFormat:@"http://%@:%ld",[HTTPServer getIPAddress],(long)http_port];
            tempLabel.alpha = 1.0f;
            theInfoLabel.alpha = 1.0f;
        } error:^{
            
        }];
    }else{
        ip_adress_label.text = [InternationalControl localizedStringForKey:@"KeyConnectError"];
        theInfoLabel.alpha = 0.0f;
        tempLabel.alpha = 0.0f;
    }

}

- (void)closeAction{
    [UIView animateWithDuration:0.3 animations:^{
        self.frame = CGRectMake(0, screenHeight, screenWidth, height_serveraction);
    }];
    [self closeServer];
}

- (void)closeServer
{
    [[SDHttpServerManager getHttpServer] stopServer];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
